import javax.xml.crypto.Data;
import java.io.*;
import java.util.Date;

import static com.sun.deploy.cache.Cache.exists;

/*
字符输入输出流
    1.读
        FileReader
            FileWriter wr = new FileWriter("1.txt");
        BufferedReader
            BufferedReader bf = new BufferedReader(new FileReader("1.txt"));
    2.写
        FileWriter
            FileWriter wr = new FileWriter("1.txt");
        BufferedWriter
            BufferedWriter bwr = new BufferedWriter(new FileWriter("1.txt.back"));
    3。从以上可看出fileXXX可提供构造，FileWriter构造时可提供第二个参数打开追加功能
        此外，BufferedReader可以读取一行数据
        使用BufferedWrite在操作结束r时需要刷新流;
        所有的资源在使用结束后要及时关闭。

 */
public class Project {
    public static void main(String[] args){
        try {
            System.out.println("FileWriter写入测试：");
            FileWriter wr = new FileWriter("fl.txt");
            wr.write("hello");
            wr.close();

            System.out.println("FileReader读取测试：");
            char[] buffer = new char[1024];
            FileReader fileReader = new FileReader("fl.txt");
            int len = fileReader.read(buffer);
            String read = new String(buffer,0,len);
            System.out.println("读取长度："+len+"内容：");
            System.out.println(read);
            fileReader.close();

            System.out.println("BufferedWriter写入测试：");
            BufferedReader bf = new BufferedReader(new FileReader("fl.txt"));
            BufferedWriter bwr = new BufferedWriter(new FileWriter("fl.txt.back"));
            String oneLine;
            while ((oneLine=bf.readLine())!= null){
                System.out.println("本行长度："+oneLine.length()+"内容："+oneLine);
                bwr.write(oneLine);
                bwr.newLine();
            }
            bwr.append("由BufferedWriter复制");
            bwr.newLine();
            bwr.flush();
            bwr.close();
            bf.close();

            /*
            System.out.println("---");
            System.out.println("获取输入字符并打印出↓");
            byte[] a = new byte[128];
            int count = System.in.read(a)-1;
            for(int t = 0;t<count;t++)
            System.out.println("count:"+count+":"+(char)a[t]);

             */

            System.out.println("---");
            System.out.println("获取当前工作目录,显示文件夹状态 ↓");
            /*
            InputStreamReader ins = new InputStreamReader(System.in);
            BufferedReader brd = new BufferedReader(ins);
            File file = new File(brd.readLine(););
             */
            File file = new File("./");
            if(file.isDirectory()){
                File filelist[] = file.listFiles();
                for(int i = 0;i < filelist.length;i++){
                    if (filelist[i].isHidden() == false) {
                        System.out.println(filelist[i].getName());
                    }
                }
            }else {
                System.out.println("输入目标是文件");
            }
            File fl = new File("fl.txt");
            if (fl.exists() == false){
                System.out.println(fl.getName()+"不存在");
                System.out.println(fl.getName()+"已创建");
                fl.createNewFile();
                FileWriter ft = new FileWriter(fl.getName());
                ft.write(fl.getName());
                ft.close();
                System.out.println(fl.getName()+"已写入");
            }
            else{
                Date date = new Date();

                System.out.println("文件fl.txt存在,向末尾追加时间");
                FileWriter ft = new FileWriter(fl.getName(),true);
                ft.write("\r\n"+date.toString());
                ft.close();
            }
        }catch (Exception e){
            System.out.println("file:"+e);
        }
    }
}